package city.spring.modules.medicalstation.doctorsOrderItem.controller;

import city.spring.modules.medicalstation.doctorsOrderItem.entity.DoctorsOrderItemEntity;
import city.spring.modules.medicalstation.doctorsOrderItem.service.DoctorsOrderItemService;
import city.spring.modules.medicalstation.doctorsOrderItem.vo.DoctorsOrderItemVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：医嘱项目
 *
 * @author 谢金婷
 * @date 2020-10-29 10:57:28
 */
@RestController
@RequestMapping("/doctors_order_item")
public class DoctorsOrderItemController extends ApiController {
    private final DoctorsOrderItemService doctorsOrderItemService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<DoctorsOrderItemEntity, ?>> entityOrderFields;

    public DoctorsOrderItemController(DoctorsOrderItemService doctorsOrderItemService) {
        this.doctorsOrderItemService = doctorsOrderItemService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(DoctorsOrderItemEntity::getId);
        entityOrderFields.add(DoctorsOrderItemEntity::getItemName);
        entityOrderFields.add(DoctorsOrderItemEntity::getType);
        entityOrderFields.add(DoctorsOrderItemEntity::getSpecification);
        entityOrderFields.add(DoctorsOrderItemEntity::getDosageUnit);
        entityOrderFields.add(DoctorsOrderItemEntity::getUnitprice);
        entityOrderFields.add(DoctorsOrderItemEntity::getGmtCreate);
        entityOrderFields.add(DoctorsOrderItemEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>医嘱项目</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, DoctorsOrderItemEntity entity) {
        LambdaQueryChainWrapper<DoctorsOrderItemEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>医嘱项目</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault @SortDefault.SortDefaults(@SortDefault(sort = "id", direction = Sort.Direction.DESC)) Pageable pageable, DoctorsOrderItemVO entity) {
//        LambdaQueryChainWrapper<DoctorsOrderItemEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<DoctorsOrderItemEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends DoctorsOrderItemEntity> queryWrapper = buildQuery(entity);
        return success(doctorsOrderItemService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    @GetMapping("/druglist")
    public Object druglist(@PageableDefault @SortDefault.SortDefaults(@SortDefault(sort = "id", direction = Sort.Direction.DESC)) Pageable pageable, DoctorsOrderItemVO entity) {
//        LambdaQueryChainWrapper<DoctorsOrderItemEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<DoctorsOrderItemEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends DoctorsOrderItemEntity> queryWrapper = buildQuery(entity);
        return success(doctorsOrderItemService.druglistPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>医嘱项目</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
//        return success(doctorsOrderItemService.getById(id));
        QueryWrapper<DoctorsOrderItemVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<DoctorsOrderItemVO> records = doctorsOrderItemService.listPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    @GetMapping("/druglist/{id}")
    public Object druglistinfo(@PathVariable String id) {
        QueryWrapper<DoctorsOrderItemVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<DoctorsOrderItemVO> records = doctorsOrderItemService.druglistPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));

//        return success(drugService.getById(id));
    }

    /**
     * 添加一个 <strong>医嘱项目</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody DoctorsOrderItemEntity entity) {
        doctorsOrderItemService.saveDoctorsOrderItem(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>医嘱项目</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody DoctorsOrderItemEntity entity) {
        entity.setId(id);
        doctorsOrderItemService.updateDoctorsOrderItem(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>医嘱项目</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        doctorsOrderItemService.deleteDoctorsOrderItem(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>医嘱项目</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        doctorsOrderItemService.deleteDoctorsOrderItem(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<DoctorsOrderItemEntity> buildLambdaQuery(DoctorsOrderItemEntity entity) {
        LambdaQueryChainWrapper<DoctorsOrderItemEntity> lambdaQuery = doctorsOrderItemService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), DoctorsOrderItemEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getItemName()), DoctorsOrderItemEntity::getItemName, entity.getItemName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), DoctorsOrderItemEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSpecification()), DoctorsOrderItemEntity::getSpecification, entity.getSpecification());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDosageUnit()), DoctorsOrderItemEntity::getDosageUnit, entity.getDosageUnit());
        lambdaQuery.eq(entity.getUnitprice() != null, DoctorsOrderItemEntity::getUnitprice, entity.getUnitprice());
        lambdaQuery.eq(entity.getGmtCreate() != null, DoctorsOrderItemEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, DoctorsOrderItemEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<DoctorsOrderItemEntity> buildQuery(DoctorsOrderItemVO entity) {
        QueryWrapper<DoctorsOrderItemEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getItemName()), "t1.item_name", entity.getItemName());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "t2.name", entity.getName());
        wrapper.like(StringUtils.isNotBlank(entity.getItemCode()), "t1.item_code", entity.getItemCode());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "t1.type", entity.getType());
        wrapper.like(StringUtils.isNotBlank(entity.getSpecification()), "t1.specification", entity.getSpecification());
        wrapper.like(StringUtils.isNotBlank(entity.getDosageUnit()), "t1.dosage_unit", entity.getDosageUnit());
        wrapper.eq(entity.getUnitprice() != null, "t1.unitprice", entity.getUnitprice());
        return wrapper;
    }

    @GetMapping("/getByType/{type}")
    public Object getByType(@PathVariable String type) {
        return success(doctorsOrderItemService.getByType(type));
    }

}
